package com.wlper.lib.view.loading;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.android.nearshop.R;
import com.wlper.lib.uitls.ViewUtils;

public class LoadingDialog extends Dialog {

    private Context mContext;

    private LayoutInflater mInflater;

    private ImageView mLoadingIcon;

    private Animation mRotateAnim;

    public LoadingDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    public LoadingDialog(Context context) {
        super(context, R.style.loading_dialog);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mInflater = ViewUtils.getInflater(mContext);
        mRotateAnim = AnimationUtils.loadAnimation(mContext, R.anim.wl_image_rotate_anim);
        View v = mInflater.inflate(R.layout.wl_view_loading_in_bg, null);
        v.setBackgroundColor(mContext.getResources().getColor(R.color.bg_dark_gray));
        v.setPadding(30, 10, 30, 10);
        mLoadingIcon = (ImageView) v.findViewById(R.id.image_v_loading);
        mLoadingIcon.setBackgroundResource(R.drawable.wl_ic_loading);
        this.setContentView(v);
        this.setCanceledOnTouchOutside(false);
    }

    @Override
    public void show() {
        if (mLoadingIcon != null)
            mLoadingIcon.startAnimation(mRotateAnim);
        super.show();
    }

    @Override
    public void dismiss() {
        if (mLoadingIcon != null)
            mLoadingIcon.clearAnimation();
        super.dismiss();
    }
}
